package lol.clann.minecraft.springboot.api.model.core;

import lombok.NonNull;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;

import java.util.HashSet;
import java.util.Set;

/**
 * 类扫描支持
 *
 * @author pyz
 * @date 2019/4/19 11:18 AM
 */
public class ClassScanner extends ClassPathScanningCandidateComponentProvider {

    public ClassScanner() {
        super(false);
    }

    public ClassScanner(@NonNull Environment environment) {
        super(false, environment);
    }

    public Set<String> doScan(String... basePackages) {
        Assert.notEmpty(basePackages, "At least one base package must be specified");
        Set<String> set = new HashSet<>();
        for (String basePackage : basePackages) {
            Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
            for (BeanDefinition candidate : candidates) {
                set.add(candidate.getBeanClassName());
            }
        }
        return set;
    }

    @Override
    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
        // 所有class全部返回
        return true;
    }
}